//
//  InfoViewController.m
//  reader
//
//  Created by mo on 2021/12/13.
//  Copyright © 2021 mo. All rights reserved.
//

#import "InfoViewController.h"
#import "UserViewController.h"
#import "SetNameController.h"
#import "SetSignController.h"
#import "Masonry.h"

@interface InfoViewController()<UITableViewDelegate,UITableViewDataSource>
@end

@implementation InfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人信息";
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"grayBack"]];
    [self addTableView];
}

//初始化时调用
- (void)setName:(NSString*)userName withImg:(UIImage*)img withSign:(NSString*)sign{
    _userName = userName;
    _userImg = img;
    _userSign = sign;
}
//发生改变时调用
- (void)setName:(NSString*)userName{
    _userName = userName;
    [self.tableView reloadData];
    [self.delegate setName:userName];
}
- (void)setSign:(NSString*)sign{
    _userSign = sign;
    [self.tableView reloadData];
    [self.delegate setSign:sign];
}


- (void) addTableView {
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    
    // 设置tableView的数据源
    _tableView.dataSource = self;
    // 设置tableView的委托
    _tableView.delegate = self;
    //禁止滚动
    _tableView.scrollEnabled = NO;
    //设置约束
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(screenBounds.size.width,240));
        make.left.equalTo(self.view.mas_left);
        make.top.equalTo(self.view.mas_top).offset(screenBounds.size.height/10);
    }];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //NSIndexPath是一个对象，记录了组和行信息
//    NSLog(@"生成单元格(组：%i,行%i)",indexPath.section,indexPath.row);
//    KCContactGroup *group=_contacts[indexPath.section];
//    KCContact *contact=group.contacts[indexPath.row];
    
    //由于此方法调用十分频繁，cell的标示声明成静态变量有利于性能优化
    static NSString *cellIdentifier=@"UITableViewCellIdentifierKey1";
    //首先根据标识去缓存池取
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    //如果缓存池没有到则重新创建并放到缓存池中
    if(!cell){
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
        
        switch (indexPath.row) {
            case 0:{
                cell.textLabel.text=@"头像";
                UIImageView* imgV = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width-130, 20, 80, 80)];
                imgV.tag = 5;
                [cell.contentView addSubview:imgV];
                break;
            }
                
            case 1:{
                cell.textLabel.text=@"名字";
                UILabel* nameL = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width-200, 10, 150, 40)];
                nameL.tag = 1;
                nameL.textAlignment = NSTextAlignmentRight;
                [cell.contentView addSubview:nameL];
                break;
            }
            
            case 2:{
                cell.textLabel.text=@"签名";
                UILabel* signL = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width-200, 10, 150, 40)];
                signL.tag = 2;
                signL.textAlignment = NSTextAlignmentRight;
                [cell.contentView addSubview:signL];
                break;
            }
                
            default:
                break;
        }
    }
    
    
    
    switch (indexPath.row) {
        case 0:{
            UIImageView* imgV = (UIImageView *)[cell.contentView viewWithTag:5];
            [imgV setImage:_userImg];
            break;
        }

        case 1:{
            UILabel* nameL = (UILabel *)[cell.contentView viewWithTag:1];
            [nameL setText:_userName];
            break;
        }

        case 2:{
            UILabel* signL = (UILabel *)[cell.contentView viewWithTag:2];
            [signL setText:_userSign];
            break;
        }

        default:
            break;
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

//返回分区数
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

//设置高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){
        return 120;
    }
    return 60;
}

//选择响应
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:{
            [self pickPhoto];
            break;
        }
            
        case 1:{
            //页面跳转
            SetNameController *next = [[SetNameController alloc]init];
            [next setDelegate:self];
            next.userName = _userName;
            [self.navigationController pushViewController:next animated:true];
            break;
        }
        
        case 2:{
            //页面跳转
            SetSignController *next = [[SetSignController alloc]init];
            [next setDelegate:self];
            next.userSign = _userSign;
            [self.navigationController pushViewController:next animated:true];
            break;
        }
            
        default:
            break;
    }
}

//选择图片
- (void)pickPhoto {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    picker.delegate = self;
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:nil];
}
//选择完成回调函数
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    _userImg = image;
    [self.tableView reloadData];
    [self.delegate setImg:image];
}

@end
